﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using Showroom.BLL;

namespace ShowroomMobile.Controls
{
    public partial class Menu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RenderMenu();
            }
        }

        private void RenderMenu()
        {
            StringBuilder str = new StringBuilder();
            str.Append("<li><a href='Default.aspx' class='level1'>Home</a></li>");
            try
            {
                View_Category cats = View_Category.GetListCategory();
                cats.Filter = View_Category.ColumnNames.ParentID + " is NULL";
                for (int i = 0; i < cats.RowCount; i++)
                {
                    str.Append(string.Format("<li><a href='Products.aspx?cid={0}' class='level1'>{1}</a>", cats.s_ID, cats.s_Name));
                    str.Append(RenderSubMenu(cats.ID));
                    str.Append("</li>");
                    cats.MoveNext();
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Menu.ascx", "RenderMenu()", ex.StackTrace);
            }
            str.Append("<li><a href='AboutUs.aspx' class='level1'>About Us</a></li>");
            str.Append("<li><a href='Contact.aspx' class='level1'>Contact</a></li>");
            ulMenu.InnerHtml = str.ToString();
        }

        private string RenderSubMenu(int parentID)
        {
            StringBuilder str = new StringBuilder();
            View_Category cats = View_Category.GetListCategory();
            cats.Filter = View_Category.ColumnNames.ParentID + "=" + parentID;
            if (cats.RowCount > 0)
            {
                str.Append("<ul>");
                for (int i = 0; i < cats.RowCount; i++)
                {
                    str.Append(string.Format("<li><a href='Products.aspx?cid={0}'>{1}</a>", cats.s_ID, cats.s_Name));
                    str.Append(RenderSubMenu(cats.ID));
                    str.Append("</li>");
                    cats.MoveNext();
                }
                str.Append("</ul>");
            }
            return str.ToString();
        }
    }
}